from typing import Optional

from requests import exceptions

from encrypt.request import send_request


class WebId:
    API = "https://mcs.zijieapi.com/webid"

    @staticmethod
    def get_web_id(user_agent: str) -> Optional[str]:
        headers = {"User-Agent": user_agent}
        data = (f'{{"app_id":6383,"url":"https://www.douyin.com/","user_agent":'
                f'"{user_agent}","referer":"https://www.douyin.com/","user_unique_id":""}}')
        try:
            if response := send_request(WebId.API, headers, data):
                return response.json().get("web_id")
            raise KeyError
        except (exceptions.JSONDecodeError, KeyError):
            print(f"获取 webid 参数失败！")
